<!--
 * @description  : 证件-责令变更通知书
 * @descriptionDetail: 证件-责令变更通知书
 * @copyright    : 浙江烟草
 * @author       : sy
 * @create       : 2022-08-15 15:29:13
-->
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar 
      :is-white="false"
      :title="navTitle"
      :custom-back="goBack"
      style="z-index: 99999"
    >
    </theme-navbar>
    <!-- #endif -->
    <view class="content">
      <u-form
        label-position="left"
        style="padding: 0px 15px;"
        :errorType="['border-bottom','message',]"
        label-width="auto"
        :model="info"
        :rules="rules"
        ref="uForm"
      >
        <u-form-item label="文书编号" prop="fullDocNo">
          <u-input type="text" input-align="right" :disabled="true" v-model="info.fullDocNo" />
        </u-form-item>
        <u-form-item label="文书日期" right-icon="arrow-right" prop="transTime">
          <u-input
            type="text"
            input-align="right"
            :disabled="true"
            v-model="info.transTime"
            @click="timeClick"
          />
        </u-form-item>
        <u-form-item label="持证人" prop="applicantName">
          <u-input type="text" input-align="right" :disabled="true" v-model="info.applicantName" />
        </u-form-item>
        <u-form-item label="许可证号" prop="licNo">
          <u-input type="text" input-align="right" :disabled="true" v-model="info.licNo" />
        </u-form-item>
        <u-form-item :required="true" label="变更内容" label-position="top" prop="stopDetail">
          <u-input type="textarea" :disabled="isDisabled" placeholder="这里输入变更内容" v-model="info.stopDetail" />
        </u-form-item>
      </u-form>
    </view>
    <!--   -->
    <view class="query-btn">
      <u-button type="primary" class="u-btn u-m-l-25 u-m-r-25" v-if="!docIns.step || docIns.step == 'edit'" @click="submitAudit('save')">
        保存
      </u-button>
      <u-button type="primary" class="u-btn" v-if="!docIns.step || docIns.step == 'edit'" @click="submitAudit('submit')">
        提交
      </u-button>
      <u-button
        type="primary"
        class="u-btn  u-m-l-25"
        v-if="!!docIns.step && docIns.step != 'edit'"
        @click="downloadFile"
      >
        打印
      </u-button>
    </view>
    <u-calendar mode="date" max-date="2099-12-31" v-model=" calenChoiceShow" @change="handlerSearchDay"></u-calendar>
  </view>
</template>

<script>
import certificateService from "@/service/certificate/certificate.service.js";
import commonService from "@/service/common/common.service";
import {LOG_STATUS,LOG_OP_TYPE,} from '@/service/common/mapping/enum.js';
export default {
  data() {
    return {
      preParams: {},
      info: {},
      calenChoiceShow: false,
      isDisabled:false,
      docIns:{},
      errorType: ['border-bottom','message',],
      beginTime:"",
      rules: {
        stopDetail: [{
          required: true,
          message: '请填写变更内容',
          trigger: 'blur',
        },],
      },
    };
  },
  onLoad() {
    this.preParams = this.getRouteParam();
  },
  mounted() {
    this.$refs.uForm.setRules(this.rules);
  },
  created() {
    this.initData();
  },
  methods: {
    initData(){
      commonService.getCurrentTime().then(res2 => {
        this.beginTime = res2.success ? res2.msg : this.$u.timeFormat(Date.now(), 'yyyy-mm-dd hh:MM:ss');
      });
      let params = {
        id: this.preParams.docInsUuid,
        docUuid:this.preParams.docUuid,
        bizUuid:this.preParams.bizUuid,
      }
      certificateService.getOrderChangeDoc(params).then((res) => {
        if(res.data.length == 1){
          this.info = res.data[0];
        }else{
          this.info = Object.assign(res.data[0],res.data[1]);
        }
      })
      certificateService.getDocIns(this.preParams.docInsUuid).then((dr) => {
        this.docIns = dr.bean;
        this.isDisabled = this.docIns.step != 'edit';
      })  
        
    },
    // 表单校验
    validForm() {
      let confirmValid = false;
      this.$refs.uForm.validate(valid => {
        if (valid) {
          confirmValid = true;
        } else {
          confirmValid = false;
        }
      });
      return confirmValid;
    },
    submitAudit(type) {
      if (!this.validForm()) return;
      this.info.handleMainUuid = this.preParams.bizUuid;
      certificateService.saveOrderChangeDoc(this.info,this.preParams.docUuid,this.preParams.docInsUuid,this.preParams.titemUuid,'edit').then((dr) => {
        if(dr.success){
          if(type === 'submit'){
            this.doSubmit();
          }else{
            this.saveBehaviorLog(0, this.docIns);
            this.$u.toast("保存成功"); 
          }
        }else{
          this.$u.toast(dr.msg);
        }
      })
    },
    doSubmit(){
      certificateService.submitOrderChangeDoc(this.info,this.preParams.docUuid,this.preParams.docInsUuid,this.preParams.titemUuid,this.docIns.step).then((res) => {
        if(res.success == true){ 
          this.$u.toast("提交成功");
          certificateService.getDocIns(this.docIns.docInsUuid).then((doc) => {
            this.docIns = doc.bean;
            this.saveBehaviorLog(1, this.docIns);
            this.isDisabled = this.docIns.step != 'edit';
          });
          this.backPrev({
            paramKey: "athResolve",
            param: this.preParams,
          });
        }else{
          this.$u.toast(res.msg);
        }
      })  
    },
    downloadFile(){
      if (!this.docIns.pdfFileId) {
        this.$u.toast("没有文书");
        return;
      }
      this.setRouteParam({
        fileId: this.docIns.pdfFileId,
      });
      this.$u.route('pages/commonPage/printerComponent');
    },
    timeClick(){
      if(this.isDisabled) return;
      this.calenChoiceShow = true;     
    },
    handlerSearchDay(e) {
      let month = '';
      let day = '';
      month = e.month < 10 ? '0' + e.month : e.month;
      day = e.day < 10 ? '0' + e.day : e.day;
      this.info.transTime = e.year + '年' + month + '月' + day + '日';
    },
    /**
	 * @param {Boolean} status 提交还是保存
	 */
    saveBehaviorLog(status, doc){
      commonService.getCurrentTime().then(res2 => {
        let param = {
          bizUuid: doc.bizUuid,
          docUuid:doc.docUuid,
          docInsUuid: doc.docInsUuid, 
          bizType:LOG_STATUS.B007,
          operationType: status == 0 ? LOG_OP_TYPE.saveDoc : LOG_OP_TYPE.submitDoc,
          beginTime: this.beginTime,
          endTime: res2.success ? res2.msg : this.$u.timeFormat(Date.now(), 'yyyy-mm-dd hh:MM:ss'),
          remark: doc.configIns.docName,
        };
        this.beginTime = param.endTime;
        commonService.saveBehaviorLog(param).then(res => {
          console.log(res);
        }).catch(e => {
          console.log(e);
        });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
    .query-btn {
        display: flex;
        position: fixed;
        width: 95%;
        z-index: 999;
        bottom: var(--window-bottom, 0);

        .u-btn {
            width: 100%;
        }
    }
</style>
